;; Benchmarks of the Optimal Bitonic Tour functions
;; Copyright (C) 2022  Rocks Mazama
;; 
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; 
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;; 
;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

(ns bench
  (:require
   [criterium.core :as criterium]
   [mazama.compgeom.optimal-bitonic-tour :refer [optimal-bitonic-tour]]))

(def WIDTH 960)
(def HEIGHT 720)

(defn- random-seq [prng count]
  (repeatedly count prng))

(defn- prng-bound [seed bound]
  (let [frand (java.util.Random. seed)]
    (fn [] (.nextInt frand bound))))

(defn- prng-min-max
  "Return a pseudo-random number generator using seed.
  Each call to the prng returns min <= value <= max."
  [seed min max]
  (let [bound (inc (- max min))
        rndm (prng-bound seed bound)]
    (fn [] (+ min (rndm)))))

(defn make-random-data [seed n]
  (map vector
       (random-seq (prng-min-max seed 0 WIDTH) n)
       (random-seq (prng-min-max seed 0 HEIGHT) n)))

(defn- run [n]
  (println \newline)
  (println "Bitonic Tour" n)
  (let [seed 17
        coll (make-random-data seed n)]
    (criterium/quick-bench (optimal-bitonic-tour coll))))

(defn -main []
  (let [ns [128 257 511 1024]]
    (doseq [n ns] (run n))))
